﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Mvc3Learning.Models;



namespace Mvc3Learning
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            System.Data.Entity.Database.SetInitializer(new BlogDbInitializer());
            

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }

    public class BlogDbInitializer : DropCreateDatabaseIfModelChanges<BlogDb>
    {
        protected override void Seed(BlogDb context)
        {
            base.Seed(context);

            IList<Post> Posts = new List<Post>();
            Posts.Add(new Post(){Content = "Primero",DateCreated = DateTime.Now,Title = "Ahora"});

            Posts.Add(new Post() { Content = "Segundo", DateCreated = DateTime.Now, Title = "Dentro de un rato " });
            Posts.Add(new Post() { Content = "Tercero", DateCreated = DateTime.Now, Title = "Mas tarde" });

            context.Blogs.Add(new Blog()
                                  {

                                      BloggerName = "www.blogspot.luciano.com",
                                      Title = "Luchaso",
                                      Posts = Posts
                                  });
            context.SaveChanges();

        }
    }
}